package com.gec.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gec.domain.User;
import com.gec.service.UserService;
import com.gec.service.impl.UserServiceImpl;

@WebServlet("/usercontrol.action")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		out.println("欢迎"+ user.getName()+"光临<br/>");
		
		//显示所有的用户信息
		UserService service = new UserServiceImpl();
		List<User> users = service.findAllUsers();
		out.println("<html>");
		out.println("<head>");
		out.println("<link rel='stylesheet' href='css/bootstrap.css'>");
		out.println("</head>");
		out.println("<body>");
		out.println("<a href='showAddUser.action' class='btn btn-info'>添加用户</a>");
		out.println("<table class='table table-border table-hover'>");
		//表头
		out.println("<tr><th>id</th><th>姓名</th><th>密码</th><th>性别</th><th>操作</th></tr>");
		
		for(User u:users) {
			//组装每一行数据
			out.println("<tr>");
			out.println("<td>" + u.getId()+"</td>");
			out.println("<td>" + u.getName()+"</td>");
			out.println("<td>" + u.getPassword()+"</td>");
			out.println("<td>" + u.getSex()+"</td>");
			// ? 是点位符
			out.println("<td><a href='delete.action?id=" + u.getId()+ "' class='btn btn-danger'>删除</a> ｜ <a href='update.action?id=" + u.getId()+"' class='btn btn-warning'>修改</a></td>");
			out.println("</tr>");
		}
		
		out.println("</table>");
		out.println("</body>");
		out.println("</html>");
		
	}

}
